<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 文章加载中... -->
    <van-loading color="#1989fa" v-if="isloading" class="loading"
      >文章疯狂加载ing...</van-loading
    >
    <div v-else>
      <!-- 文章信息区域 -->
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{ artObj.title }}</h1>

        <!-- 用户信息 -->
        <van-cell center :title="artObj.aut_name" :label="artObj.pubdate">
          <template #icon>
            <img :src="artObj.aut_photo" alt="" class="avatar" />
          </template>
          <template #default>
            <div>
              <van-button
                type="info"
                size="mini"
                v-if="artObj.is_followed"
                @click="followedFn(false)"
                >已关注</van-button
              >
              <van-button
                icon="plus"
                type="info"
                size="mini"
                plain
                v-else
                @click="followedFn(true)"
                >关注</van-button
              >
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div class="art-content" v-html="artObj.content"></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞 -->
        <div class="like-box">
          <van-button
            v-if="artObj.attitude === 1"
            icon="good-job"
            type="danger"
            size="small"
            @click="loveFn(false)"
            >已点赞</van-button
          >
          <van-button
            v-else
            icon="good-job-o"
            type="danger"
            plain
            size="small"
            @click="loveFn(true)"
            >点赞</van-button
          >
        </div>
      </div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        offset="50"
        :immediate-check="false"
      >
        <CommentList
          v-for="item in commentList"
          :key="item.com_id"
          :obj="item"
          :isShowCmtInput="isShowCmtInput"
        >
        </CommentList>
      </van-list>
    </div>
    <!-- 底部添加评论区域 - 1 -->
    <div class="add-cmt-box van-hairline--top" v-if="isShowCmtInput">
      <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
      <div class="ipt-cmt-div" @click="isShowCmtInput = false">发表评论</div>
      <div class="icon-box">
        <van-badge :content="totalCount" max="99">
          <van-icon name="comment-o" size="0.53333334rem" @click="moveFn" />
        </van-badge>
        <van-icon name="star-o" size="0.53333334rem" />
        <van-icon name="share-o" size="0.53333334rem" />
      </div>
    </div>

    <!-- 底部添加评论区域 - 2 -->
    <div class="cmt-box van-hairline--top" v-else>
      <textarea
        v-model="comValue"
        v-focus
        placeholder="友善评论、理性发言、阳光心灵"
        @blur="onblur"
      ></textarea>
      <van-button
        type="default"
        :disabled="comValue.length === 0"
        @click="submit"
        >发布</van-button
      >
    </div>
  </div>
</template>

<script>
import {
  articleDetailAPI,
  followedUserAPI,
  unFollowedUserAPI,
  articleLikeAPI,
  articleDisLikeAPI,
  commentListAPI,
  sendCommentAPI
} from '@/api'
import CommentList from './CommentList.vue'
export default {
  name: 'ArticleDetail',
  data () {
    return {
      artObj: {},
      commentList: [],
      totalCount: 0,
      isShowCmtInput: true,
      comValue: '',
      loading: false,
      finished: false,
      // 最后一条评论的id
      offset: null,
      isloading: false
    }
  },
  components: {
    CommentList
  },
  async created () {
    this.isloading = true
    const res = await articleDetailAPI({ id: this.$route.query.aid })
    this.artObj = res.data.data
    this.getCommentListFn()
    this.isloading = false
  },
  methods: {
    async getCommentListFn () {
      const res1 = await commentListAPI({
        source: this.$route.query.aid,
        offset: this.offset
      })

      if (res1.data.data.results.length === 0) {
        this.finished = true
      } else {
        this.commentList = [...this.commentList, ...res1.data.data.results]
        this.totalCount = res1.data.data.total_count
        this.offset = res1.data.data.last_id
        this.loading = false
      }
    },
    onLoad () {
      this.getCommentListFn()
    },
    onblur () {
      setTimeout(() => {
        this.isShowCmtInput = true
      })
    },
    async submit () {
      const res = await sendCommentAPI({
        target: this.$route.query.aid,
        content: this.comValue
      })
      this.commentList.unshift(res.data.data.new_obj)
      this.totalCount++
      this.comValue = ''
    },
    // 点击评论按钮 滚动到底部
    async moveFn () {
      document.querySelector('.like-box').scrollIntoView({
        behavior: 'smooth'
      })
    },
    // 关注作者uid
    async followedFn (val) {
      this.artObj.is_followed = val
      if (val) {
        // 关注
        await followedUserAPI({
          target: this.artObj.aut_id
        })
        // 取消关注
      } else {
        await unFollowedUserAPI({
          uid: this.artObj.aut_id
        })
      }
    },
    // 点赞
    async loveFn (val) {
      if (val) {
        this.artObj.attitude = 1
        await articleLikeAPI({
          target: this.$route.query.aid
        })
      } else {
        this.artObj.attitude = -1
        await articleDisLikeAPI({
          artId: this.$route.query.aid
        })
      }
    }
  }
}
</script>

<style scoped lang="less">
.loading {
  margin-top: 50px;
  text-align: center;
}
.article-container {
  padding: 10px;
  margin-top: 46px;
}

.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  text-align: left;

  /deep/ img {
    width: 100%;
  }

  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;

  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
